package com.lovo.project.service.impl;

import com.lovo.project.dao.PetOwnDAO;
import com.lovo.project.enums.PetOwnEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.PetOwnDO;
import com.lovo.project.service.PetOwnService;
import com.lovo.project.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;

/**
 * @author 955
 * @title 宠主业务实现类
 * @date 2020/11/3 20:23
 */
public class PetOwnServiceImpl implements PetOwnService {
    @Override
    public void insertPetOwn(PetOwnDO po) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        PetOwnDAO dao=sqlSession.getMapper(PetOwnDAO.class);
        try{
            int isInsert=dao.insertPetOwn(po);
            if(isInsert<=0){
                throw new OurException(PetOwnEnum.PET_OWN_INSERTEXP_ENUM);
            }
            sqlSession.commit();
        }catch (OurException o){
            sqlSession.rollback();
            throw o;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public void deletePetOwn(Integer id) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        PetOwnDAO dao=sqlSession.getMapper(PetOwnDAO.class);
        try{
            int isDel=dao.deletePetOwn(id);
            if(isDel<=0){
                throw new OurException(PetOwnEnum.PET_OWN_DELETEEXP_ENUM);
            }
            sqlSession.commit();
        }catch (OurException o){
            sqlSession.rollback();
            throw o;
        }finally {
            sqlSession.close();
        }

    }

    @Override
    public PetOwnDO selectById(Integer id) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        PetOwnDAO dao=sqlSession.getMapper(PetOwnDAO.class);
        try{
            PetOwnDO po=dao.selectById(id);
            if(po==null) {
                throw new OurException(PetOwnEnum.PET_OWN_FINDEXP_ENUM);
            }
            return po;
        }catch (OurException o){
            throw o;
        }finally {
            sqlSession.close();
        }
    }
    @Override
    public List<PetOwnDO> selectAll(String type, String value) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        PetOwnDAO dao=sqlSession.getMapper(PetOwnDAO.class);
        try{
           List<PetOwnDO>list=dao.selectAll(type,value);
            if(list==null){
                throw new OurException(PetOwnEnum.PET_OWN_FINDEXP_ENUM);
            }
            return list;
        }catch (OurException o){
            throw o;
        }finally {
            sqlSession.close();
        }

    }

    @Override
    public void update(PetOwnDO po) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        PetOwnDAO dao=sqlSession.getMapper(PetOwnDAO.class);
        try{
            int isUpdate=dao.update(po);
            if(isUpdate<=0){
                throw new OurException(PetOwnEnum.PET_OWN_UPDATEEXP_ENUM);
            }
            sqlSession.commit();
        }catch (OurException o){
            sqlSession.rollback();
            throw o;
        }finally {
            sqlSession.close();
        }

    }
}
